<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="SHZY-dialog SHZY-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        size="small"
        label-width="100px"
        label-position="right"
        :rules="rules"
      >
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="dataForm.name"
              placeholder="请输入"
              clearable
              required
              :style="{ width: '100%' }"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="年度" prop="year">
            <el-input
              v-model="dataForm.year"
              placeholder="请输入"
              clearable
              required
              :style="{ width: '100%' }"
              maxlength="4"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="开始时间" prop="startTime">
            <el-time-picker
              v-model="dataForm.startTime"
              placeholder="请选择"
              clearable
              required
              :style="{ width: '100%' }"
              format="HH:mm:ss"
              value-format="HH:mm:ss"
              :picker-options="{ selectableRange: '00:00:00-23:59:59' }"
            >
            </el-time-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="结束时间" prop="endTime">
            <el-time-picker
              v-model="dataForm.endTime"
              placeholder="请选择"
              clearable
              required
              :style="{ width: '100%' }"
              format="HH:mm:ss"
              value-format="HH:mm:ss"
              :picker-options="{ selectableRange: '00:00:00-23:59:59' }"
            >
            </el-time-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="有效标志" prop="enabledMark">
            <el-switch
              v-model="dataForm.enabledMark"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import restAPI from "@/api/q1/basedata/worktime";
export default {
  components: {},
  props: [],
  data() {
    return {
      btnLoading: false,
      loading: false,
      visible: false,
      dataForm: {
        id: "",
        name: undefined,
        year: undefined,
        startTime: undefined,
        endTime: undefined,
        enabledMark: 1,
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: '"blur"',
          },
        ],
        year: [
          {
            required: true,
            message: "请输入年度",
            trigger: '"blur"',
          },
          {
            pattern: /^\d+$/,
            message: "请输入正确的年度",
            trigger: '"blur"',
          },
        ],
        startTime: [
          {
            required: true,
            message: "请输入开始时间",
            trigger: '"change"',
          },
        ],
        endTime: [
          {
            required: true,
            message: "请输入结束时间",
            trigger: '"change"',
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    goBack() {
      this.$emit("refresh");
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          restAPI.getInfo(this.dataForm.id).then((res) => {
            this.dataForm = res.data;
          });
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.startTime > this.dataForm.endTime) {
            this.$message({
              type: "warning",
              message: "结束时间不能小于开始时间",
              onClose: () => {},
            });
            return;
          }

          this.btnLoading = true;
          if (!this.dataForm.id) {
            restAPI.create(this.dataForm).then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.btnLoading = false;
                  (this.visible = false), this.$emit("refresh", true);
                },
              });
            });
          } else {
            restAPI.update(this.dataForm).then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.btnLoading = false;
                  this.visible = false;
                  this.$emit("refresh", true);
                },
              });
            });
          }
          this.btnLoading = false;
        }
      });
    },
  },
};
</script>
